package gui1;


import java.awt.*; 
import java.awt.event.*; 
import java.util.Vector; 
 
import javax.swing.*; 
 
import java.sql.*; 
 
/*class Goods{ 
 public String[] name; 
 public Float[] price; 
 Goods(){ 
  name =new String[100]; 
  price=new Float[100]; 
 } 
} 
public class SuperMarket extends JFrame{ 
 public static void main(String[] args) throws SQLException{ 
  MainWinow mainWin=new MainWinow("���ϳ��й������");   
  mainWin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  mainWin.setBounds(300, 300, 500, 400); 
  mainWin.setVisible(true); 
  mainWin.setWin(mainWin); 
  mainWin.setMinWindowLayout(); 
 } 
} 
 
class MainWinow extends JFrame{ 
 Goods goods; 
 private JButton user; 
 private JButton manager; 
 private JLabel loginLabel; 
 private ManageWindow magWin; 
 private UserWindow userWin; 
 private Listener lis; 
 private MainWinow loginWin; 
 private int goodsNum; 
  
 * ���ý��� 
 *  
 private JLabel setNameLabel; 
 private JLabel setPriceLabel; 
 private JTextField setNameText; 
 private JTextField setPriceText; 
 private JButton inputBut; 
 private TextArea inputArea; 
 private JButton returnBut1; 
 private JButton cancelBut; 
  
 * �û����� 
 *  
 private Vector<String> buyItem; 
 private Float[] buyCount; 
 private int buyNum; 
 private JComboBox goodsCombox; 
 private JButton returnBut2; 
 private JLabel choiceGoodLabel; 
 private JLabel showPriceLabel; 
 private JTextField showPrice; 
 private TextArea showChoice; 
 private JLabel showBuyNum; 
 private JTextField showBuyNumText; 
 private JButton submitBuy; 
 private JButton deleteBuyBut; 
 private JList choiceList; 
 private JButton countBut; 
 private Float sumMoney; 
 *//** 
  * ���ݿ⵼�� 
  *//* 
  Statement stmt; 
 MainWinow(String winName) throws SQLException{ 
  super(winName); 
  goodsNum=0; 
  buyNum=0; 
  sumMoney=(float)0; 
  goods=new Goods(); 
  user=new JButton("�����û�"); 
  manager=new JButton("���ǹ���Ա"); 
  loginLabel=new JLabel("��ѡ���ɫ!"); 
  magWin=new ManageWindow("������Ʒ"); 
  magWin.setBounds(300, 300, 500, 400); 
  magWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
  userWin=new UserWindow("��ӭѡ��"); 
  userWin.setBounds(300, 300, 500, 400); 
  userWin.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
  lis=new Listener(); 
   
  * ���ý����ʼ�� 
  *  
  setNameLabel=new JLabel("��Ʒ��:"); 
  setPriceLabel=new JLabel("�۸�:"); 
  setNameText=new JTextField(5); 
  setPriceText=new JTextField(5); 
  inputBut=new JButton("ȷ�����"); 
  inputArea=new TextArea(); 
  returnBut1=new JButton("����"); 
  cancelBut=new JButton("�������"); 
   
  * �û������ʼ�� 
  *  
  goodsCombox=new JComboBox(); 
  returnBut2=new JButton("����"); 
  choiceGoodLabel=new JLabel("��ѡ����Ʒ:"); 
  showPriceLabel=new JLabel("�۸�"); 
  showPrice=new JTextField(5); 
  showChoice=new TextArea(); 
  showBuyNum=new JLabel("��������:"); 
  showBuyNumText=new JTextField(5); 
  submitBuy=new JButton("ȷ�Ϲ���"); 
  deleteBuyBut=new JButton("ɾ������"); 
  countBut=new JButton("��������"); 
  choiceList=new JList(); 
  buyItem=new Vector<String>(); 
  buyCount=new Float[100]; 
   
   * ���ݿ�ĵ��� 
   *  
  try { 
   Class.forName("com.mysql.jdbc.Driver"); 
  } catch (ClassNotFoundException e) { 
   // TODO Auto-generated catch block 
   e.printStackTrace(); 
  } 
	String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false"; 
  String user="root"; 
  String password="87552624xy"; 
  Connection con=DriverManager.getConnection(url,user,password); 
  stmt = con.createStatement(); 
   
   * ���ݿ����ݵĵ��� 
   *  
  importSql(); 
 } 
 public void importSql() throws SQLException { 
  int i=1; 
  // TODO Auto-generated method stub 
  ResultSet result=stmt.executeQuery( 
    "SELECT name,price FROM goods_info" 
    ); 
  while(result.next()){ 
   goods.name[i-1]=result.getString(1); 
   goods.price[i-1]=Float.parseFloat(result.getString(2)); 
   i++; 
  } 
  goodsNum=i-1; 
 } 
 public void setWin(MainWinow w){ 
  loginWin=w; 
 } 
 public void setMinWindowLayout(){ 
  Container loginCon=new Container(); 
  loginCon.setLayout(new FlowLayout()); 
  loginCon.add(manager); 
  loginCon.add(user); 
   
  manager.addActionListener(lis); 
  user.addActionListener(lis); 
  this.setLayout(new BorderLayout()); 
  this.add(loginLabel,BorderLayout.NORTH); 
  this.add(loginCon,BorderLayout.CENTER); 
  this.validate(); 
   
  * ���ý��沼�� 
  *  
  magWin.setLayout(new FlowLayout()); 
  magWin.add(setNameLabel); 
  magWin.add(setNameText); 
  magWin.add(setPriceLabel); 
  magWin.add(setPriceText); 
  magWin.add(inputBut); 
  magWin.add(inputArea); 
  magWin.add(cancelBut); 
  magWin.add(returnBut1); 
   
  inputBut.addActionListener(lis); 
  returnBut1.addActionListener(lis); 
  cancelBut.addActionListener(lis); 
   
  * �û����沼�� 
  *  
  userWin.setLayout(new BorderLayout()); 
  Container userCon=new Container(); 
  userCon.setLayout(new FlowLayout()); 
  userCon.add(choiceGoodLabel); 
  userCon.add(goodsCombox); 
  userCon.add(showPriceLabel); 
  userCon.add(showPrice); 
  userCon.add(showBuyNum); 
  userCon.add(showBuyNumText); 
  userCon.add(submitBuy); 
  userWin.add(userCon,BorderLayout.NORTH); 
  //choiceList.setListData(goods.name); 
  userWin.add(choiceList,BorderLayout.CENTER); 
  userWin.add(new JScrollPane(choiceList)); 
   
  Container butCon=new Container(); 
  butCon.setLayout(new FlowLayout()); 
  butCon.add(deleteBuyBut); 
  butCon.add(countBut); 
  butCon.add(returnBut2); 
  userWin.add(butCon,BorderLayout.SOUTH); 
   
  goodsCombox.addItemListener( 
   new ItemListener(){ 
   @Override 
    public void itemStateChanged(ItemEvent e) { 
    // TODO Auto-generated method stub 
    int i=goodsCombox.getSelectedIndex(); 
    if(i>=0)showPrice.setText(goods.price[i].toString()); 
   } 
  } 
  ); 
  returnBut2.addActionListener(lis); 
  submitBuy.addActionListener(lis); 
  deleteBuyBut.addActionListener(lis); 
  countBut.addActionListener(lis); 
 } 
  
 private void addComboxItem() { 
  // TODO Auto-generated method stub 
  for(int i=0;i<goodsNum;i++){ 
   goodsCombox.addItem(goods.name[i]); 
  } 
 } 
  
 class Listener implements ActionListener{ 
  @Override 
   public void actionPerformed(ActionEvent e) { 
   // TODO Auto-generated method stub 
   if(e.getSource()==manager){ 
    addGoods(); 
    loginWin.setVisible(false); 
    magWin.setVisible(true); 
   } 
   if(e.getSource()==user){ 
    loginWin.setVisible(false); 
    userWin.setVisible(true); 
    goodsCombox.removeAllItems(); 
    addComboxItem(); 
   } 
   if(e.getSource()==inputBut){ 
    //String showOut=""; 
     
    if(setNameText.getText().equals("")||setPriceText.getText().equals("")){ 
     JOptionPane.showMessageDialog(magWin,"�������п�����!", "����",JOptionPane.PLAIN_MESSAGE); 
    } 
    else{ 
     goods.name[goodsNum]=setNameText.getText(); 
     goods.price[goodsNum]=Float.parseFloat(setPriceText.getText()); 
     try { 
       
       * д�����ݿ� 
       *  
      stmt.executeUpdate("insert into goods_info(name,price)values('"+goods.name[goodsNum]+"','"+(float)goods.price[goodsNum]+"')"); 
     } catch (SQLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     goodsNum++; 
     addGoods(); 
     setNameText.setText(""); 
     setPriceText.setText(""); 
     //showOut="��Ʒ��:"+setNameText.getText()+"\t"+"�۸�:"+setPriceText.getText()+"\n"; 
     //inputArea.append(showOut); 
    } 
   } 
   if(e.getSource()==cancelBut){ 
    if(goodsNum>0){ 
     goodsNum--; 
     String deleteName=goods.name[goodsNum]; 
     String deletePrice=goods.price[goodsNum].toString(); 
     //System.out.println(deleteName); 
      
      * ɾ�����ݿ������Ԫ�� 
      *  
     String sql = "delete from goods_info where name = '"+deleteName+"' AND price ='"+deletePrice+"'"; 
     try { 
      stmt.executeUpdate(sql); 
     } catch (SQLException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     //Connection con= DBManager .getConnection();; 
     //PreparedStatement ps = con.prepareStatement(sql); 
     addGoods(); 
    } 
   } 
   if(e.getSource()==returnBut1){ 
    loginWin.setVisible(true); 
    magWin.setVisible(false); 
   } 
    
   * �û������¼���Ӧ 
   *  
   if(e.getSource()==returnBut2){ 
    loginWin.setVisible(true); 
    userWin.setVisible(false); 
   } 
   if(e.getSource()==submitBuy){ 
     if(!showBuyNumText.getText().equals("")){ 
      buyCount[goodsCombox.getSelectedIndex()]=Float.parseFloat(showBuyNumText.getText()); 
     String contentItem=""; 
     Float sumMon=Float.parseFloat(showBuyNumText.getText())*(Float)goods.price[goodsCombox.getSelectedIndex()]; 
     contentItem="��Ʒ����"+goods.name[goodsCombox.getSelectedIndex()]+" " 
      +"���ۣ�"+goods.price[goodsCombox.getSelectedIndex()].toString()+" " 
      +"����������"+showBuyNumText.getText()+" " 
      +"�ܼۣ�"+sumMon.toString(); 
     buyItem.addElement(contentItem); 
     //buyItem[buyNum]=contentItem; 
     buyNum++; 
     choiceList.removeAll(); 
     choiceList.setListData(buyItem); 
     sumMoney+=sumMon; 
    } 
    else{ 
     JOptionPane.showMessageDialog(magWin,"��������������Ϊ��", "����",JOptionPane.PLAIN_MESSAGE); 
    } 
     
   } 
   if(e.getSource()==deleteBuyBut){ 
    if(choiceList.getSelectedValue()==null){ 
     JOptionPane.showMessageDialog(magWin,"δѡ���ɾ������Ʒ", "����",JOptionPane.PLAIN_MESSAGE);     
    } 
    else if(buyNum>0){ 
     int i=choiceList.getSelectedIndex(); 
     String selectItem=buyItem.get(i); 
     //System.out.println(selectItem); 
     String deletePrice=""; 
     for(int j=0;j<selectItem.length()-3;j++){ 
     // System.out.println(selectItem.substring(j, j+3)); 
      if(selectItem.substring(j, j+3).equals("�ܼۣ�")){ 
       deletePrice=selectItem.substring(j+3,selectItem.length()); 
       System.out.println(deletePrice); 
       sumMoney-=Float.parseFloat(deletePrice); 
       break; 
      } 
     } 
     buyItem.remove(i); 
     choiceList.removeAll(); 
     choiceList.setListData(buyItem); 
     choiceList.validate(); 
     buyNum--; 
    } 
    else{ 
     JOptionPane.showMessageDialog(magWin,"���ﳵΪ��,����ɾ��", "����",JOptionPane.PLAIN_MESSAGE); 
    } 
   } 
   if(e.getSource()==countBut){//sumMoney 
    for(int i=0;i<buyItem.size();i++){ 
     String str=buyItem.get(i).substring(0, 2); 
     if(str.equals("�ܼ�")){ 
      buyItem.remove(i); 
     } 
    } 
    buyItem.addElement("�ܼۣ�"+sumMoney.toString()); 
    choiceList.removeAll(); 
    choiceList.setListData(buyItem); 
    choiceList.validate(); 
   } 
  } 
   
  private void addGoods() { 
   if(!inputArea.getText().equals(""))inputArea.setText(""); 
   // TODO Auto-generated method stub 
   for(int i=0;i<goodsNum;i++){ 
    String massage="��Ʒ��:"+goods.name[i]+"\t"+"�۸�:"+goods.price[i].toString()+"\n"; 
    inputArea.append(massage); 
   } 
  } 
 } 
} 
class ManageWindow extends JFrame { 
 ManageWindow(String title){ 
  super(title); 
 } 
} 
class UserWindow extends JFrame{ 
 UserWindow(String title){ 
  super(title); 
 } 
} */